<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use App\Model\{Product,Productcat,Productattr,Cart,Config,Red};


class Order extends Model
{
    public static function add_order($p){
    	$ids = $p['ids'];
    	unset($p['ids']);
        $red_id = $p['red_id']??0;
        unset($p['red_id']);
    	$cart_list = Cart::cart_list($p['uid'],$ids);
    	if(empty($cart_list)){
    		return false;
    	}
    	$order = $p;
    	$order['add_time'] = time();
    	$order['pay_status']=0;
    	$order['ship_status'] = 0;
    	$order['order_no'] = order_no();
        $order['amount'] = $cart_list->total_amount;

        $discount = Red::discount($p['uid'],$order['amount']);
        if($discount['id']>0){
            $order['amount'] = $order['amount']-$discount['discount'];
            $order['discount'] = $discount['discount'];
        }

    	DB::beginTransaction();
    	$id = DB::table('order')->insertGetId($order);
    	$order_product = [];
    	foreach($cart_list->items() as $item){
    		$order_product[] = [
    			'order_id'=>$id,
    			'product_id'=>$item->product_id,
    			'product_name'=>$item->product_name,
    			'img'=>$item->img,
    			'number'=>$item->number,
    			'unit_price'=>$item->unit_price,
    			'sub_price'=>$item->price,
    			'attr_id'=>$item->attr_id,
    			'attr'=>$item->attr,
    		];
    	}
    	$bool = DB::table('order_product')->insert($order_product);
    	$del = DB::table('cart')->whereIn('id',$ids)->delete();

    	if($id && $bool && $del){
    		DB::commit();
            #红包状态
            if($discount['id']>0){
                DB::table('user_red')->where('id',$discount['id'])->update([
                    'status'=>1,
                    'use_time'=>time(),
                    'src_id'=>$id,
                    'actual_money'=>$discount['discount'],
                ]);
            }
            Cache::forget('user:cart:common:'.$p['uid']);
            
    		return [
    			'order_no'=>$order['order_no'],
    		];
    	}else{
    		DB::rollBack();
    		return false;
    	}
    }


    public static function order_list($p=[]){
        $db = DB::table('order')->select(['*']);
        if(!empty($p['uid'])){
            $db->where('uid',$p['uid']);
        }
        if(!empty($p['email'])){
            $db->where('uid',$p['email']);
        }
        if(!empty($p['order_no'])){
            $db->where('order_no',$p['order_no']);
        }
        if(isset($p['pay_status']) && $p['pay_status']!==""){
            $db->where('pay_status',$p['pay_status']);
        }
        if(isset($p['ship_status']) && $p['ship_status']!==""){
            $db->where('ship_status',$p['ship_status']);
        }

        $list = $db->orderBy('id','desc')->paginate(10,['id']);
        $list->withPath('/order/index');
        $ids = get_array_keys($list->items(),'id');
        $products = DB::table('order_product')->whereIn('order_id',$ids)->get()->toArray();

        $uids = get_array_keys($list->items(),'uid');
        $users = DB::table('userdata')->select('id','nickname','phone')->whereIn('id',$uids)->get()->toArray();
        $users = arrays_set_key($users,'id');

        $goods = [];
        foreach($products as $item){
            $item->imgs = explode('|',$item->img);
            $item->img = $item->imgs[0];
            $goods[$item->order_id][] = $item;
        }

        $order_pay_status = Config::get_config_bykey('order_pay_status');
        $order_ship_status = Config::get_config_bykey('order_ship_status');
        foreach($list->items() as &$line){
            $line->product = $goods[$line->id]??[];
            $line->pay_status_text = $order_pay_status[$line->pay_status];
            $line->ship_status_text = $order_ship_status[$line->ship_status];

            $line->user = $users[$line->uid];
        }
        return $list;
    }
}
